package com.hopu.blb_erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopu.blb_erp.entity.*;
import com.hopu.blb_erp.mapper.CustomerMapper;
import com.hopu.blb_erp.mapper.SCompanyMapper;
import com.hopu.blb_erp.service.CustomerService;
import com.hopu.blb_erp.service.TRelationService;
import com.hopu.blb_erp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private SCompanyMapper sCompanyMapper;

    @Autowired
    private TRelationService tRelationService;

    @Autowired
    private UserService userService;

    @Transactional
    @Override
    public void saveCustomerCompany(SCompany company) {
        LocalDateTime now = LocalDateTime.now();
        company.setCreatTime(now);
        int l = (int) System.currentTimeMillis();
        company.setCompanyId(l);
        sCompanyMapper.insert(company);

        Customer customer = new Customer();
        customer.setName(company.getCompanyName());
        customer.setAddress(company.getCompanyProvince() + company.getCompanyCity() + company.getCompanyArea() + company.getCompanyAddress());
        customer.setBusiness(company.getBusinessScope());
        customer.setEmail(company.getCompanyEmail());
        customer.setTelephone(company.getFixedTel());
        customerMapper.insert(customer);
        SCompany sCompany = sCompanyMapper.selectCompanyByName(company.getCompanyName());
        sCompany.setCompanyId(sCompany.getId());
        sCompanyMapper.updateById(sCompany);

        TRelation tRelation = new TRelation();
        tRelation.setCustomerId(sCompany.getId());
        //通过工具类的user的username查询user实体类的user
        org.springframework.security.core.userdetails.User user =
                (org.springframework.security.core.userdetails.User)
                        SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User byName = userService.findByName(user.getUsername());
        tRelation.setSupplierId(byName.getCompanyId());
        tRelationService.save(tRelation);

    }



    @Override
    public PageEntity<Customer> findCustomerPage(int current, int limit, String name, String address,String cName) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        if (name != null ){
            queryWrapper.like("name",name);
        }
        if (address != null){
            queryWrapper.like("address",address);
        }
        int count = Math.toIntExact(customerMapper.selectCount(queryWrapper.ne("name",cName)));
        int pageSize = count % limit == 0 ? count / limit : count / limit + 1;
        List<Customer> customers = customerMapper.selectCustomerPage((current - 1) * limit,limit,name,address,cName);
        PageEntity<Customer> page = new PageEntity<>();
        page.setCount(count);
        page.setData(customers);
        page.setPageSize(pageSize);
        page.setCurrent(current);
        return page;
    }
}
